package com.nodecollege.cloud.controller.admin;

import com.nodecollege.cloud.common.annotation.AdminLoginAnnotation;
import com.nodecollege.cloud.common.annotation.ApiAnnotation;
import com.nodecollege.cloud.common.constants.NCConstants;
import com.nodecollege.cloud.common.model.NCResult;
import com.nodecollege.cloud.common.model.QueryVO;
import com.nodecollege.cloud.common.model.po.UpmsApp;
import com.nodecollege.cloud.common.model.po.UpmsAppResource;
import com.nodecollege.cloud.common.model.po.UpmsMenu;
import com.nodecollege.cloud.common.model.vo.MenuTreeVO;
import com.nodecollege.cloud.service.AppService;
import com.nodecollege.cloud.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 应用管理
 *
 * @author LC
 * @date 2019/12/18 11:05
 */
@RestController
@AdminLoginAnnotation
@RequestMapping("/app")
public class AppController {

    @Autowired
    private AppService appService;

    @Autowired
    private MenuService menuService;

    /**
     * 查询应用列表
     *
     * @param queryVO
     */
    @ApiAnnotation(modularName = "应用管理", description = "查询应用列表")
    @PostMapping("/getAppList")
    public NCResult<UpmsApp> getAppList(@RequestBody QueryVO<UpmsApp> queryVO) {
        return appService.getAppList(queryVO);
    }

    /**
     * 查询应用列表缓存
     */
    @ApiAnnotation(modularName = "应用管理", description = "查询应用列表缓存")
    @PostMapping("/getAppListCache")
    public NCResult<UpmsApp> getAppListCache() {
        return NCResult.ok(appService.getAppListCache());
    }

    /**
     * 清除应用列表缓存
     */
    @ApiAnnotation(modularName = "应用管理", description = "清除应用列表缓存")
    @RequestMapping("/clearAppListCache")
    public NCResult<UpmsApp> clearAppListCache() {
        appService.clearAppListCache();
        return NCResult.ok("清除缓存成功");
    }

    /**
     * 添加应用
     *
     * @param upmsApp
     * @return
     */
    @ApiAnnotation(modularName = "应用管理", description = "添加应用")
    @PostMapping("/addApp")
    public NCResult addApp(@RequestBody UpmsApp upmsApp) {
        appService.addApp(upmsApp);
        return NCResult.ok("添加应用成功！");
    }

    /**
     * 修改应用
     *
     * @param upmsApp
     * @return
     */
    @ApiAnnotation(modularName = "应用管理", description = "修改应用")
    @PostMapping("/updateApp")
    public NCResult updateApp(@RequestBody UpmsApp upmsApp) {
        appService.updateApp(upmsApp);
        return NCResult.ok("修改应用成功！");
    }


    /**
     * 删除应用
     *
     * @param upmsApp
     * @return
     */
    @ApiAnnotation(modularName = "应用管理", description = "删除应用")
    @PostMapping("/delApp")
    public NCResult delApp(@RequestBody UpmsApp upmsApp) {
        appService.delApp(upmsApp.getAppId());
        return NCResult.ok("删除应用成功！");
    }

    @ApiAnnotation(modularName = "应用管理", description = "获取应用菜单树")
    @PostMapping("/getMenuTreeByAppId")
    public NCResult<MenuTreeVO> getMenuTreeByAppId(@RequestBody UpmsMenu menu) {
        menu.setMenuSource(NCConstants.INT_0);
        return NCResult.ok(menuService.getMenuTree(menu));
    }

    @ApiAnnotation(modularName = "应用管理", description = "查询应用菜单列表")
    @PostMapping("/getMenuListByAppId")
    public NCResult<UpmsAppResource> getMenuListByAppId(@RequestBody UpmsMenu menu) {
        menu.setMenuSource(NCConstants.INT_0);
        return NCResult.ok(menuService.getMenuList(menu));
    }

    /**
     * 添加应用资源
     *
     * @param menu
     * @return
     */
    @ApiAnnotation(modularName = "应用管理", description = "添加应用菜单")
    @PostMapping("/addMenuByAppId")
    public NCResult addMenuByAppId(@RequestBody UpmsMenu menu) {
        menu.setMenuSource(NCConstants.INT_0);
        menuService.addMenu(menu);
        return NCResult.ok("添加应用菜单成功！");
    }

    /**
     * 修改应用资源
     *
     * @param menu
     * @return
     */
    @ApiAnnotation(modularName = "应用管理", description = "修改应用菜单")
    @PostMapping("/updateMenuByAppId")
    public NCResult updateMenuByAppId(@RequestBody UpmsMenu menu) {
        menu.setMenuSource(NCConstants.INT_0);
        menuService.updateMenu(menu);
        return NCResult.ok("修改应用菜单成功！");
    }

    /**
     * 删除应用资源
     *
     * @param menu
     * @return
     */
    @ApiAnnotation(modularName = "应用管理", description = "删除应用菜单")
    @PostMapping("/delMenuByAppId")
    public NCResult delMenuByAppId(@RequestBody UpmsMenu menu) {
        menu.setMenuSource(NCConstants.INT_0);
        menuService.delMenu(menu);
        return NCResult.ok("删除应用菜单成功！");
    }
}
